<template>
  <div>
    <!-- 文章列表 -->
    <!-- 下拉刷新 -->
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
    <van-list
     v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
    offset="50"
    :immediate-check="false"
    >
    <article-item v-for="(item,index) in articleList" :key="index" :obj="item">
    </article-item>
    </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from './components/ArticleItem.vue'
import { articlesListAPI } from '@/api'
import { timeAgo } from '@/utils/date'
export default {
  data () {
    return {
      articleList: [],
      loading: false,
      finished: false,
      nextTime: null
    }
  },
  props: {
    cid: Number
  },
  components: {
    ArticleItem
  },
  methods: {
    // 底部加载封装方法
    async getArticleList () {
      const res = await articlesListAPI({
        channelId: this.cid,
        timestamp: this.nextTime
      })
      res.data.data.results.forEach((item) => {
        item.pubdate = timeAgo(item.pubdate)
      })
      if (res.data.data.results.length === 0) {
        this.finished = true
      } else {
        this.nextTime = res.data.data.pre_timestamp
        this.articleList = [...this.articleList, ...res.data.data.results]
        this.loading = false
      }
      // this.articleList = res.data.data.results
    },
    onLoad () {
      if (this.articleList.length > 0) {
        this.getArticleList()
      }
    },
    // 下拉刷新
    onRefresh () {
      this.nextTime = null
      this.articleList = []
      this.getArticleList()
      this.isLoading = false
    }
  },
  async created () {
    this.getArticleList()
  }
}
</script>
